package com.ndp.fb.walle.permission;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.mq.api.producer.RocketMqProducerService;
import com.ndp.fb.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hunk.qin on 2015/7/27.
 */
public class UserActionTrack implements Runnable {

    private static Logger logger = LoggerFactory.getLogger(UserActionTrack.class);

    private RocketMqProducerService rocketMqProducerService;

    private String username, userOrg, controller, func;

    public UserActionTrack(RocketMqProducerService rocketMqProducerService, String username, String userOrg, String controller, String func) {
        this.rocketMqProducerService = rocketMqProducerService;
        this.username = username;
        this.userOrg = userOrg;
        this.controller = controller;
        this.func = func;
    }

    @Override
    public void run() {
        sendUserAction();
    }

    private void sendUserAction(){
        try {
            String topicName = ConfigConst.MQ_SERVICES_USER_ACTION_TOPIC;
            String tagName = topicName + ConfigConst.ROCKETMQ_TOPIC_TAG_SPLIT + ConfigConst.MQ_SERVICES_USER_ACTION_TAG;
            String keys = UUID.get();
            Map<String ,String> actionParams=new HashMap<>();
            actionParams.put("USERNAME",username);
            actionParams.put("USERGROUP",userOrg);
            actionParams.put("CONTROLLER",controller);
            actionParams.put("FUNC",func);
            rocketMqProducerService.sendMessage(topicName, tagName, keys, JSON.toJSON(actionParams));
        }catch (Exception e){
//            logger.error("send user action to mq error :"+e.getMessage());
//            logger.error("",e);
        }
    }
}
